package com.example.controller;

import com.example.common.RequirePermission;
import com.example.common.Result;
import com.example.dto.WorkScheduleDateDTO;
import com.example.service.WorkScheduleDateService;
import com.example.vo.WorkScheduleDateVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 日期排班控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/work-schedule-date")
@RequiredArgsConstructor
public class WorkScheduleDateController {
    
    private final WorkScheduleDateService workScheduleDateService;
    
    /**
     * 添加日期排班
     */
    @PostMapping("/add")
    @RequirePermission("store:schedule:add")
    public Result<Void> addWorkScheduleDate(@RequestBody WorkScheduleDateDTO workScheduleDateDTO) {
        return workScheduleDateService.addWorkScheduleDate(workScheduleDateDTO);
    }

    /**
     * 更新日期排班
     */
    @PutMapping("/update/{id}")
    @RequirePermission("store:schedule:update")
    public Result<Void> updateWorkScheduleDate(@PathVariable Long id, @RequestBody WorkScheduleDateDTO workScheduleDateDTO) {
        return workScheduleDateService.updateWorkScheduleDate(id, workScheduleDateDTO);
    }

    /**
     * 删除日期排班
     */
    @DeleteMapping("/delete/{id}")
    @RequirePermission("store:schedule:delete")
    public Result<Void> deleteWorkScheduleDate(@PathVariable Long id) {
        return workScheduleDateService.deleteWorkScheduleDate(id);
    }

    /**
     * 根据ID查询日期排班详情
     */
    @GetMapping("/{id}")
    @RequirePermission("store:schedule:view")
    public Result<WorkScheduleDateVO> getWorkScheduleDateById(@PathVariable Long id) {
        return workScheduleDateService.getWorkScheduleDateById(id);
    }
    
    /**
     * 获取我的日期排班列表
     */
    @GetMapping("/my-schedules")
    @RequirePermission("store:schedule:view")
    public Result<List<WorkScheduleDateVO>> getMyWorkScheduleDates() {
        return workScheduleDateService.getMyWorkScheduleDates();
    }

    /**
     * 根据日期获取我的日期排班列表
     */
    @GetMapping("/my-schedules/date/{scheduleDate}")
    @RequirePermission("store:schedule:view")
    public Result<List<WorkScheduleDateVO>> getMyWorkScheduleDatesByDate(
            @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate scheduleDate) {
        return workScheduleDateService.getMyWorkScheduleDatesByDate(scheduleDate);
    }

    /**
     * 根据日期范围获取我的日期排班列表
     */
    @GetMapping("/my-schedules/date-range")
    @RequirePermission("store:schedule:view")
    public Result<List<WorkScheduleDateVO>> getMyWorkScheduleDatesByDateRange(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        return workScheduleDateService.getMyWorkScheduleDatesByDateRange(startDate, endDate);
    }

    /**
     * 批量设置日期排班
     */
    @PostMapping("/batch-set")
    @RequirePermission("store:schedule:add")
    public Result<Void> batchSetWorkScheduleDates(@RequestBody List<WorkScheduleDateDTO> workScheduleDateDTOs) {
        return workScheduleDateService.batchSetWorkScheduleDates(workScheduleDateDTOs);
    }

    /**
     * 清空指定日期的排班
     */
    @DeleteMapping("/clear-date/{scheduleDate}")
    @RequirePermission("store:schedule:delete")
    public Result<Void> clearScheduleByDate(@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate scheduleDate) {
        return workScheduleDateService.clearScheduleByDate(scheduleDate);
    }
    
    /**
     * 获取指定日期的已排班时间段
     */
    @GetMapping("/occupied-time-slots/{scheduleDate}")
    @RequirePermission("store:schedule:view")
    public Result<List<Integer[]>> getOccupiedTimeSlotsForDate(
            @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate scheduleDate) {
        return workScheduleDateService.getOccupiedTimeSlotsForDate(scheduleDate);
    }
    
    /**
     * 从周排班模板复制到指定日期
     */
    @PostMapping("/copy-from-weekly/{scheduleDate}")
    @RequirePermission("store:schedule:add")
    public Result<Void> copyFromWeeklyTemplate(@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate scheduleDate) {
        return workScheduleDateService.copyFromWeeklyTemplate(scheduleDate);
    }
} 